class Base{
    isEmpty(param){
        //数组  对象
        //instanceof   Array.isArray(); 
        if(Array.isArray(param)){
            //数组
            return param.length ? param.length : '数组为空'
        }else{
            //对象
            let keys = Object.keys(param);
            return keys ? keys : '对象是空'
        }
    }
}

const fs = require('fs');

const path = require('path');

class Util extends Base{
    //加载js
    loadJs(path){
        return this.readFile(path,'utf8')
    }
    //加载css
    loadCss(path){
        return this.readFile(path,'utf8')
    }
    //加载img
    loadImg(path){
        return this.readFile(path,'base64')
    }

    //读文件
    readFile(path,type){
        return new Promise((resovle,reject) => {
            fs.readFile(path,type,(error,data) => {
                if(error){
                    //失败
                    reject(error);
                }else{
                    //成功
                    resovle(data);
                }
            })
        })
    }

    
    async init(files){
        let arr = [];
        for(let i = 0;i<files.length;i++){
            // files[i]
            let extname = path.extname(files[i]).slice(1);  //js  css jpg
            console.log(extname) ;//loadCss
            extname = /^(jpg|png)$/.test(extname) ? 'img' : extname;
            
            let type = 'load'+extname.substr(0,1).toUpperCase()+extname.substring(1);

            //loadJs  loadCss  loadImg

            // if(extname === 'js'){
            //     this.loadJs()
            // }else if(extname === 'css'){
            //     this.loadCss()
            // }else{
            //     this.loadImg()
            // }

            // this[type]

            console.log(type);
            let data = await this[type](files[i]);
            arr.push(data);
        }
        return arr
    }
}

let util = new Util();


util.init(['./src/index.js','./src/index.css','./src/1.jpg']).then(res => {
    console.log(res);
})

//作业：1.加载文件的class案例  2.找弹窗的类练习  3.promise封装以及promise的使用